import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store';
import Home from '@/components/Home' // 主页面
import ErrorPage from '@/components/error/ErrorPage' // 404页面
import Login from '@/components/login/Login' // 登录页面

import ManagerAcademy from "@/components/manager/ManagerAcademy" // 班级管理
import ManagerClass from "@/components/manager/ManagerClass" // 班级管理
import ManagerDormitory from "@/components/manager/ManagerDormitory" // 宿舍管理
import ManagerGroup from "@/components/manager/ManagerGroup" // 分组管理
import UserList  from '@/components/student/UserList' // 用户列表页面
import UserAdd  from '@/components/student/UserAdd' // 添加用户信息页面
import UserEdit  from '@/components/student/UserEdit' // 编辑用户信息页面
Vue.use(Router)
let router = new Router({
  routes: [
    {// 默认路由
      path:"/",
      redirect:"/login"
    },
    {// 内容路由
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/home',
      name: 'Home',
      redirect:"/home/managerAcademy",
      component: Home,
      children: [
        {
          path: 'managerAcademy',
          name: 'ManagerAcademy', // 学院管理
          component: ManagerAcademy,
          meta: {
            requireLogin:true,
          }
        },
        {
          path: 'managerClass',
          name: 'ManagerClass', // 班级管理
          component: ManagerClass,
          meta: {
            requireLogin:true,
          }
        },
        {
          path: 'managerDormitory',
          name: 'ManagerDormitory', // 宿舍管理
          component: ManagerDormitory,
          meta: {
            requireLogin:true,
          }
        },
        {
          path: 'managerGroup',
          name: 'ManagerGroup', // 分组用户
          component: ManagerGroup,
          meta: {
            requireLogin:true,
          }
        },
        {
          path:'userList',
          name:'UserList', // 用户列表
          component: UserList,
          meta: {
            requireLogin:true,
          }
        },
        {
          path:'userAdd',
          name:'UserAdd', // 添加用户
          component: UserAdd,
          meta: {
            requireLogin:true,
          }
        },
        {
          path: 'userEdit',
          name: 'UserEdit', // 编辑用户
          component: UserEdit,
          meta: {
            requireLogin:true,
          }
        }

      ]
    },
    {//其它未定义路由
      path:'*',
      name:'ErrorPage',
      component: ErrorPage
    }
  ]
})


//路由拦截:登录拦截
router.beforeEach((to,from,next) => {
  if(to.meta.requireLogin){
    if(store.state.loginStatus){
      next()
    }else{
      next({
        path: '/login',
        query:{redirect: to.fullPath}
      })
    }
  }else{
    next();
  }
});


export default router;
